//
//  RSI.m
//  AppFinance
//
//  Created by wei on 12-7-15.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AFRSI.h"

@implementation AFRSI

static AFRSI *_rsi = nil;

static float _kpiMinRsi = 100000000;
static float _kpiMaxRsi = 0;
static float _kpiLastRsiM6 = 0;
static float _kpiLastRsiM12 = 0;

+(AFRSI*) app{
    if (_rsi == nil) {
        _rsi = [[self alloc] init];
    }
    return _rsi;
}

+(float) kpiMinRsi{
    return _kpiMinRsi;
}
+(float) kpiMaxRsi{
    return _kpiMaxRsi;
}
+(float) kpiLastRsiM6{
    return _kpiLastRsiM6;
}
+(float) kpiLastRsiM12{
    return _kpiLastRsiM12;
}

int rsi_iParam[] = {6,12};


-(void) initKlineArray:(NSArray *)_kLineArray{
    _kpiMaxRsi = 0;
    _kpiLastRsiM6 = 0;
    _kpiLastRsiM12 = 0;
    kLineArray = _kLineArray;
    
    _kpiMinRsi = 100000000;
    _kpiMaxRsi = 0;
}

-(NSMutableArray *) rsiData:(int) day drawCount:(int)_drawCount{
    
    int n = day;
    
    if(n > [kLineArray count]|| n < 1){
        return nil;
    }
    
    NSMutableArray *rsiArray = [NSMutableArray array];
    
    float pre_close = 0;
    float close = 0;
    
    float up = 0.0f;
    float down = 0.0f;
    
    NSDictionary *dict;
    
    dict = [kLineArray objectAtIndex:0];
    pre_close = [[dict objectForKey:AF_K_MARKET_CLOSE] floatValue];
    
    // 获取第一个 rsi
    for(int i = 1; i < n; i++){
        dict = [kLineArray objectAtIndex:i];
        close = [[dict objectForKey:AF_K_MARKET_CLOSE] floatValue];
        if (close > pre_close) {
            up += close - pre_close;
        } else {
            down += pre_close - close;
        }
        pre_close = close;
    }
    
    // 0 － n-1 添加为 0
    for (int i = 0; i< n-1; i++) {
        [rsiArray addObject:[NSNumber numberWithFloat:0]];
    }
    
    //float rsi[] = {};
    float _rsi = 0;
    if(up + down == 0.0f){
        [rsiArray addObject:[NSNumber numberWithFloat:50]];
    }
    else{
        _rsi = (up / (up + down)) * 100;
        [rsiArray addObject:[NSNumber numberWithFloat:_rsi]];
    }
    
    float predown;
    float preup = predown = 0.0;
    
    float close1 = 0;
    float close2 = 0;
    for(int i = n; i < [kLineArray count]; i++) {
        dict = [kLineArray objectAtIndex:i];
        close = [[dict objectForKey:AF_K_MARKET_CLOSE] floatValue];
        
        // 计算当前的 up 和 down
        up -= preup;
        down -= predown;
        if(close > pre_close){
            up += close - pre_close;
        }
        else{
            down += pre_close - close;
        }
        pre_close = close;
        
        // 计算 rsi
        if(up + down == 0.0){
            _rsi = [[rsiArray objectAtIndex:(i - 1)] floatValue];
        }
        else{
            _rsi = (up / (up + down)) * 100.0f;
        }
        
        // 获取 max rsi
        if(_rsi < _kpiMinRsi){
            _kpiMinRsi = _rsi;
        }
        if(_rsi > _kpiMaxRsi){
            _kpiMaxRsi = _rsi;
        }
        
        //[rsiArray insertObject:[NSNumber numberWithFloat:_rsi] atIndex:(n-1)];
        [rsiArray addObject:[NSNumber numberWithFloat:_rsi]];
        
        // 计算最前面 的 up 和 down
        preup = predown = 0.0;
        dict = [kLineArray objectAtIndex:((i - n) + 1)];
        close1 = [[dict objectForKey:AF_K_MARKET_CLOSE] floatValue];
        dict = [kLineArray objectAtIndex:(i - n)];
        close2 = [[dict objectForKey:AF_K_MARKET_CLOSE] floatValue];
        
        if(close1 > close2){
            preup = close1 - close2;
        }else{
            predown = close2 - close1;
        }
    }
    return rsiArray;
}

@end
